<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        var data = {
            id: 1,
            name: '家电',
            goods: [
                { id: 11, gname: '冰箱' },
                { id: 12, gname: '洗衣机' }
            ],
            brand: {
                name: '海尔'
            }
        }
        
        // 浅拷贝
        // 方法1：
        function qkb(obj){
            var qian = {};
            for(var key in obj){
                qian[key] = obj[key];
            }
            return qian;
        }

        var qianData = qkb(data);
        qianData.id = 2;
        console.log(qianData);
        console.log(data);
        qianData.brand.name = '美的';
        console.log(qianData);
        console.log(data);
        // 方法2：
        // Object.assign(target, source);

        console.log('--------分割线--------');

        var data1 = {
            id: 1,
            name: '家电',
            goods: [
                { id: 11, gname: '冰箱' },
                { id: 12, gname: '洗衣机' }
            ],
            brand: {
                name: '海尔',
                type: {
                    name: '冰箱'
                }
            }
        }

        // 深拷贝
        var skbData = {};
        function skb(newObj, oldObj){
            for(var k in oldObj){
                if(oldObj.hasOwnProperty(k)){
                    var item = oldObj[k];
                    if(item instanceof Array){
                        newObj[k] = [];
                        skb(newObj[k], item);
                    } else if (item instanceof Object){
                        newObj[k] = {};
                        skb(newObj[k], item);
                    } else{
                        newObj[k] = item;
                    }
                }
            }
        }
        skb(skbData, data1);
        skbData.brand.name = '美的';
        console.log(skbData);
        console.log(data1);

    </script>
</body>
</html>